package com.alit.face.koala.controller;

import java.util.Collection;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alit.face.koala.projectvariable.ProjectVariable;
import com.litong.utils.projectvariable.PVU;

import lombok.extern.slf4j.Slf4j;

/**
 * @author litong
 * @date 2019年5月9日_上午9:48:55
 * @version 1.0
 */
@RestController
@RequestMapping("/project/variable")
@Slf4j
public class ProjectVariableController {

  @RequestMapping("/list")
  public String getVariables() {
    Map<String, ProjectVariable> maps = PVU.getParameters();
    Collection<ProjectVariable> values = maps.values();
    return JSON.toJSONString(values);
  }

  @RequestMapping({ "save", "update" })
  public String save(String key, String value, String comment, boolean readOnly) {
    log.info("更新配置变量:" + key + "," + value + "" + comment);
    PVU.add(key, comment, value, readOnly, true);
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("success", true);
    return jsonObject.toJSONString();
  }

  // 不允许用户删除
  // @RequestMapping("destroy")
  // public String destroy(String id) {
  // PVU.destroy(id);
  // // {'success':'true'}
  // JSONObject jsonObject = new JSONObject();
  // jsonObject.put("success", true);
  // return jsonObject.toJSONString();
  // // return "{'success':'true'}";
  // }
}
